﻿/**
* @file action_14032.c
* @brief this is a brief description
* @details this is the detail description.
* @author fulong 
* @data 2015/1/6
* @version 1.0
* @par Copyright (c):
* @par First build
*     version: fulong 
*     data: 2015/1/6
*/
#include "action_14032.h"
#include "../../../../dev/input.h"
#include "../../action.h"
#include "../../../../lib/delay.h"
#include "../../../../app/menu/menu_common.h"
#include "../../../../lib/pid.h"
#include "../../../temperture.h"
#include "math.h"
#ifdef __XT14032__
extern PID_struct pid;
extern input_status_process status_process;
extern display_text_widget_struct all_page_dis_text1;

extern display_text_widget_struct watch_dis_text0;
extern display_text_widget_struct watch_dis_text1;
extern display_text_widget_struct watch_dis_text2;
typedef enum {
    PLUSE_HIGH,
    PLUSE_LOW
}p_status;
double temp;
uint8 pluse_status = PLUSE_LOW;
uint8 temp_error = 0;
uint8 err = 0;
    #define PID_UK_MAX pid.ratio
    #define PID_UK_MIN 0
void action_14032(void)
{
    pid.setpoint = read_action_param(0);
    pid.ratio = (double)read_action_param(4);
    pid.aim_value = (double)read_action_param(5);
    pid.proportion = (double)read_action_param(1) / 10;
    pid.integral = (double)read_action_param(2) / 10;
    pid.derivative = (double)read_action_param(3) / 10;
    pid.pid_min = pid.setpoint - 6;
    pid.pid_max = pid.setpoint + 2;
    pid.get_error_value = (int32(*)())HAL_get_temperture_value;
    apply4delay(ACTION_DELAY_FLAG1, 200);
    if (get_delay_flag(ACTION_DELAY_FLAG1))
    {
        temp = pid.get_error_value() / 10.0;
        all_page_dis_text1.dis_var = temp * 10;
    }
    apply4delay(ACTION_DELAY_FLAG, pid.ratio * 10);
    if (get_delay_flag(ACTION_DELAY_FLAG))
    {
        pid.uk += inc_pid_process(&err, temp);
        if (pid.uk >= PID_UK_MAX)
        {
            pid.uk = PID_UK_MAX;
        } else if (pid.uk <= PID_UK_MIN)
        {
            pid.uk = PID_UK_MIN;
        }
        watch_dis_text0.dis_var = ((uint16)pid.uk / 100) % 10;
        watch_dis_text1.dis_var = ((uint16)pid.uk / 10) % 10;
        watch_dis_text2.dis_var = (uint16)pid.uk % 10;
    }
    if (err != 0xFF)
    {
        if (temp >= pid.pid_min && temp <= pid.pid_max)
        {
            temp_error = 2;
            if (pluse_status == PLUSE_LOW)
            {
                Y01 = 0;
                apply4delay(ACTION_DELAY_FLAG3,  ((uint16)pid.ratio - (uint16)pid.uk) * 10);
                if (get_delay_flag(ACTION_DELAY_FLAG3))
                {
                    pluse_status = PLUSE_HIGH;
                }
            } else if (pluse_status == PLUSE_HIGH)
            {
                Y01 = 1;
                apply4delay(ACTION_DELAY_FLAG2,  ((uint16)pid.uk) * 10);
                if (get_delay_flag(ACTION_DELAY_FLAG2))
                {
                    pluse_status = PLUSE_LOW;
                }
            }
        } else if (temp < (pid.pid_min - temp_error))
        {
            temp_error = 0;
            Y01 = 1;
            pid.uk = PID_UK_MIN;
        } else if (temp > pid.pid_max)
        {
            temp_error = 0;
            Y01 = 0;
            pid.uk = PID_UK_MIN;
        }
    }
}
#endif

